﻿using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Linq;

namespace iPhoneUI.Shell
{
    public class ListConfig : Config
    {
        private Type m_itemType;

        public ListConfig(XmlNode config)
            : base(config)
        {
            m_itemType = Type.GetType(GetValue("@OfType"));
        }

        public IList Create()
        {
            IList list = CreateListInstance();

            foreach (ObjectConfig config in ListContents)
                list.Add(config.CreateInstance());

            return list;
        }

        public IEnumerable<ObjectConfig> ListContents
        {
            get
            {
                return GetNode().Convert<ObjectConfig>(n => new ObjectConfig(n), "object");
            }
        }

        private IList CreateListInstance()
        {
            Type type = typeof(List<>);
            Type t = type.MakeGenericType(m_itemType);
            return Activator.CreateInstance(t) as IList;
        }
    }
}
